package com.tarena.lbs.user.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.api.BusinessApi;
import com.tarena.lbs.business.api.pojo.BusinessDto;
import com.tarena.lbs.common.enums.PassportResultEnum;
import com.tarena.lbs.user.dao.mapper.AdminMapper;
import com.tarena.lbs.user.dao.repository.AdminRepository;
import com.tarena.lbs.user.pojo.param.AdminLoginParam;
import com.tarena.lbs.user.pojo.param.AdminParam;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.query.AdminQuery;

import com.tarena.lbs.user.pojo.vo.AdminVO;
import com.tarena.lbs.user.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminRepository adminRepository;

    @Autowired
    AdminMapper adminMapper;






    @Override
    public AdminVO login(AdminLoginParam param) throws BusinessException {
        AdminPO adminPO=adminRepository.login(param);
        if (adminPO==null){
            throw new BusinessException(PassportResultEnum.ADMIN_NONE_EXIST);
        }
        if (!param.getPassword().equals(adminPO.getAccountPassword())){
            throw  new BusinessException(PassportResultEnum.ADMIN_PASSWORD_ERROR);
        }
        AdminVO adminVO=new AdminVO();
        BeanUtils.copyProperties(adminPO,adminVO);
        return  adminVO;
    }

    @Override
    public AdminVO detail(Integer id) {
        AdminPO adminPO=adminRepository.detail(id);
        AdminVO adminVO=new AdminVO();
        BeanUtils.copyProperties(adminPO,adminVO);
        return adminVO;
    }

    @Override
    public Integer getType(Integer id) {
        Integer  type = adminMapper.getType(id);
        return type;
    }

    @Override
    public void save(AdminParam adminParam) throws BusinessException {
        log.debug("保存账号参数:{}", adminParam);

        AdminPO adminPO=new AdminPO();
        // 检查手机号是否为空
        if (StringUtils.isEmpty(adminParam.getAccountPhone())){
            throw new BusinessException(PassportResultEnum.PHONE_NUMBER_EMPTY);
        }
        // 新增：用户名非空校验
        if (StringUtils.isBlank(adminParam.getNickname())) {
            throw new BusinessException(PassportResultEnum.USERNAME_EMPTY);
        }

        // 查询数据库判断手机号是否已注册
        AdminPO existingUser = adminRepository.findByPhone(adminParam.getAccountPhone());
        if (existingUser != null) {
            throw new BusinessException(PassportResultEnum.USER_ALREADY_EXISTS);
        }
        Integer accountType1 = adminParam.getAccountType();
        //账号类型为平台管理员
        if (accountType1==0){
            BeanUtils.copyProperties(adminParam,adminPO);
            adminPO.setBusinessId(0);
            adminPO.setCreateTime(new Date());
            log.debug("保存账号参数:{}", adminPO);
            adminRepository.save(adminPO);
        }
        //账号类型为商家账号
        if (accountType1==1){
            BeanUtils.copyProperties(adminParam,adminPO);
            adminPO.setCreateTime(new Date());
            adminRepository.save(adminPO);
        }

    }

    @Override
    public PageResult<AdminVO> adminList(AdminQuery query) {
        log.debug("查询账号列表参数:{}", query);
        PageResult<AdminVO> list = adminRepository.list(query);
        return list;
    }

//    @Override
//    public void save(AdminParam param) {
//        log.debug("保存账号参数:{}", param);
//        AdminPO adminPO = new AdminPO();
//        BeanUtils.copyProperties(param, adminPO);
//        adminPO.setCreateTime(new Date());
//        adminMapper.save(adminPO);
//    }

    @Override
    public int selectByBusinessId(Integer id) {
        Integer count = adminMapper.selectByBusinessId(id);
        return count;
    }

    @Override
    public int selectByPhone(String accountPhone) {
        Integer count = adminMapper.selectByPhone(accountPhone);
        return count;
    }


}
